import java.util.Stack;

public class topic {

        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            for(int i =0;i<s.length();++i){
                char ch = s.charAt(i);
                if(ch == '(' || ch == '{' || ch=='['){
                    //左括号
                    stack.push(ch);
                }else{
                    //右括号
                    //判断只有右括号
                    if(stack.empty()){
                        return false;
                    }
                    char top = stack.peek();
                    //此时top是左括号，ch是右括号
                    if(ch == ')' && top == '(' || ch == ']' && top == '[' || ch == '}' && top =='{'){
                        stack.pop();
                    }else{
                        return false;
                    }
                }
            }
            //防止还有左括号没有匹配，而以及没有有括号了

            if(!stack.empty()){
                return false;
            }
            return true;

        }
        //逆波兰表达式
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i=0;i<tokens.length;++i){
            String s = tokens[i];
            if(isSign(s)){
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(s){
                    case "+":
                        stack.push(num1 + num2);
                        break;
                    case "-":
                        stack.push(num1 - num2);
                        break;
                    case "*":
                        stack.push(num1 * num2);
                        break;
                    case "/":
                        stack.push(num1 / num2);
                        break;
                }

            }else{
                //如果是数字字符就转为Integer存入
                stack.push(Integer.parseInt(s));
            }
        }
        return stack.pop();

    }
    //判断是否为运算符
    public boolean isSign(String s){
        if(s.equals("+") || s.equals("-") || s.equals("*") || s.equals("/")){
            return true;
        }else{
            return false;
        }


    }

}
